package com.scalar.chiptrack.utils;

import java.util.ArrayList;

/**
 * This is a utility ArrayList class. This class just overrides the get(int) method of LinkedList.<br>
 * It just creates the object specified in the cunstructor and returns if the object not present at the specified index.<br>
 * The main purpose of creating this class is to avoid the scope <br>session</br> when we used lists with nested tags.
 * 
 * User: Sujan
 * Date: Nov 2, 2005
 * Time: 3:22:31 PM
 */
public class AutoGrowingArrayList extends ArrayList
{
	private Class clazz;
	private boolean isAutoGrowingEnabled = true; //default AutoGrowing is Enabled
	public AutoGrowingArrayList ( Class clazz )
	{
		super();
		this.clazz = clazz;
	}
    public AutoGrowingArrayList ( Class clazz, boolean isAutoGrowingEnabled )
	{
		super();
		this.clazz = clazz;
		this.isAutoGrowingEnabled = isAutoGrowingEnabled;
	}
	public Object get ( int index )
	{
		Object obj = null;
		if ( isAutoGrowingEnabled )
		{
			int size = super.size();
			if ( index < size )
			{
				obj = super.get ( index );
			}
			else
			{
				for ( int i = size; i <= index; i++ )
				{
					obj = null;
					try
					{
						obj = this.clazz.newInstance ();
					}
					catch ( Exception e )
					{
						System.out.println ( "-----exception in AutoGrowingLinkedList.get(.)--" );
						e.printStackTrace();
					}
					super.add ( obj );
				}
			}
		}
		else
		{
			obj = super.get ( index );
		}
		return obj;
	}
	public boolean isAutoGrowingEnabled ()
	{
		return isAutoGrowingEnabled;
	}
}
